package com.bt.lms.network.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bt.lms.bo.StudentList;
import com.bt.lms.constant.StuStatusBizTypeConstant;
import com.bt.lms.entity.*;
import com.bt.lms.entity.page.Page;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.service.*;
import com.bt.lms.service.impl.*;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

/**
 * 学籍信息模块处理器接口实现
 *
 * @author 王波
 */
public class StuStatusModuleHandler implements ModuleHandler {
    @Override
    public void handlerMsg(ReqMsg reqMsg, Socket socket) {

        //1。获取类型
        Short bizType = reqMsg.getBizType();
        //2。处理参数
        String data = reqMsg.getData();
        //调用业务接口
        Object obj = bizManage(bizType, data);
        if (obj != null) {
            //如果需要响应
            Byte twoWay = reqMsg.getTwoWay();
            if (twoWay == ReqMsg.NETWORK_TWO_WAY_CHAT) {
                try {
                    //写流
                    OutputStream out = socket.getOutputStream();
                    RespMsg respMsg = objReqMsg(reqMsg, obj);
                    byte[] bytes = respMsg.toBytes();
                    out.write(bytes);
                    //冲刷
                    out.flush();


                    //关闭资源
                    out.close();
                    socket.close();

                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }

        }


        //响应
    }

    /**
     * 对象转为响应消息
     *
     * @param reqMsg 请求消息
     * @param obj    响应消息
     * @return 响应消息
     */
    private RespMsg objReqMsg(ReqMsg reqMsg, Object obj) {

        //要返回的对象
        RespMsg respMsg = new RespMsg();

        //组装响应消息，1.消息头
        //魔数
        String magicNo = reqMsg.getMagicNo();
        respMsg.setMagicNo(magicNo);
        //协议版本号
        Byte version = reqMsg.getVersion();
        respMsg.setVersion(version);
        //序列化算法 serialCou
        Byte serialCou = reqMsg.getSerialCou();
        respMsg.setSerialCou(serialCou);
        //事件类型 evenType
        Byte evenType = reqMsg.getEvenType();
        respMsg.setEvenType(evenType);
        //业务类型 bizType
        Short bizTypeBytes = reqMsg.getBizType();
        respMsg.setBizType(bizTypeBytes);
        //请求id 毫秒数 reqId
        Long reqId = reqMsg.getReqId();
        respMsg.setReqId(reqId);
        //保留字段 remainField
        byte[] remainField = reqMsg.getRemainField();
        respMsg.setRemainField(remainField);
        //状态  status
        respMsg.setStatus(RespMsg.NETWORK_STATUS_NORMAL);
        //数据 data
        String data = JSON.toJSONString(obj);
        respMsg.setData(data);

        return respMsg;
    }

    /**
     * 调用业务方法
     *
     * @param bizType 业务类型
     * @param data    消息体内容
     * @return 对象
     */
    private Object bizManage(Short bizType, String data) {

        Object obj = null;
        //查看详情业务接口
        StuStatusInfoService stuStatusInfoService = new StuStatusInfoServiceImpl();
        //学员列表
        if (bizType.equals(StuStatusBizTypeConstant.INFO_BIZ_TYPE)) {
            Page studentListPage = JSONObject.parseObject(data, Page.class);
            try {
                obj = stuStatusInfoService.infoQuery(studentListPage);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        //学员基本信息
        StuBasicInfoServiceImpl stuBasicInfoService = new StuBasicInfoServiceImpl();
        if (bizType.equals(StuStatusBizTypeConstant.BASIC_INFO_BIZ_TYPE)) {
            StuBasicInfo stuBasicInfo = JSON.parseObject(data, StuBasicInfo.class);
            Long id = stuBasicInfo.getId();
            try {
                obj = stuBasicInfoService.edit(id, stuBasicInfo);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            String picture = stuBasicInfo.getPicture();
            //编辑照片
            try {
                obj = stuBasicInfoService.editHeadImage(id, picture);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            //删除头像
            if (picture == null) {
                try {
                    obj = stuBasicInfoService.deleteHeadImage(id);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }

            }

        }
        //家庭成员业务接口
        StuFamilyMemberService stuFamilyMemberService = new StuFamilyMemberServiceImpl();

        if (bizType.equals(StuStatusBizTypeConstant.FAMILY_MEMBER_BIZ_TYPE_EDIT)) {
            //编辑家庭成员
            FamilyMemberInfo familyMemberInfo = JSONObject.parseObject(data, FamilyMemberInfo.class);
            Long id = familyMemberInfo.getId();
            try {
                //调用编辑的业务接口
                obj = stuFamilyMemberService.edit(id, familyMemberInfo);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        if (bizType.equals(StuStatusBizTypeConstant.FAMILY_MEMBER_BIZ_TYPE_DELETE)) {
            //删除家庭成员
            FamilyMemberInfo familyMemberInfo = JSONObject.parseObject(data, FamilyMemberInfo.class);
            Long id = familyMemberInfo.getId();

            //调用编辑的业务接口
            try {
                obj = stuFamilyMemberService.delete(id);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        if (bizType.equals(StuStatusBizTypeConstant.FAMILY_MEMBER_BIZ_TYPE_ADD)) {
            //添加家庭成员
            //将消息内容转换为对象
            FamilyMemberInfo familyMemberInfo = JSONObject.parseObject(data, FamilyMemberInfo.class);
            Long id = familyMemberInfo.getId();
            try {
                obj = stuFamilyMemberService.add(id, familyMemberInfo);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        //学籍基本信息
        StuStatusBasicInfoService stuStatusBasicInfoService = new StuStatusBasicInfoServiceImpl();
        if (bizType.equals(StuStatusBizTypeConstant.STU_STATUS_BASIC_INFO_BIZ_TYPE_EDIT)) {
            StuStatusBasicInfo stuStatusBasicInfo = JSONObject.parseObject(data, StuStatusBasicInfo.class);
            Long id = stuStatusBasicInfo.getId();
            try {
                obj = stuStatusBasicInfoService.edit(id, stuStatusBasicInfo);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

        if (bizType.equals(StuStatusBizTypeConstant.QUERY_PAGING_CONDITION_BIZ_TYPE)) {
            //客户端传过来的条件对象
            ListConditionSearchInfo listConditionSearchInfo = JSONObject.parseObject(data, ListConditionSearchInfo.class);
            //获取排序类型
            Page<StudentList> studentListPage = new Page<>();
            try {
                obj = stuStatusInfoService.pagingConditionQuery(studentListPage);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }
        if (bizType.equals(StuStatusBizTypeConstant.QUERY_DETAILS_BIZ_TYPE)) {
            StuBasicInfo stuBasicInfo = JSONObject.parseObject(data, StuBasicInfo.class);
            Long id = stuBasicInfo.getId();
            try {
                StuBasicInfo details = stuStatusInfoService.findDetails(id);
                List<FamilyMemberInfo> familyMemberInfos = details.getFamilyMemberInfos();
                obj = familyMemberInfos;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        }
        //注册信息
        StuStatusRegisterInfoService stuStatusRegisterInfoService = new StuStatusRegisterInfoServiceImpl();
        if (bizType.equals(StuStatusBizTypeConstant.REGISTER_INFO_BIZ_TYPE_EDIT)) {
            RegisterInfo registerInfo = JSONObject.parseObject(data, RegisterInfo.class);
            Long id = registerInfo.getId();
            try {
                obj = stuStatusRegisterInfoService.edit(id, registerInfo);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (bizType.equals(StuStatusBizTypeConstant.REGISTER_INFO_BIZ_TYPE_DELETE)) {
            RegisterInfo registerInfo = JSONObject.parseObject(data, RegisterInfo.class);
            Long id = registerInfo.getId();
            try {
                obj = stuStatusRegisterInfoService.delete(id);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        if (bizType.equals(StuStatusBizTypeConstant.REGISTER_INFO_BIZ_TYPE_ADD)) {
            //将JSON消息转为异动entity对象
            RegisterInfo registerInfo = JSONObject.parseObject(data, RegisterInfo.class);
            Long id = registerInfo.getId();
            try {
                //调用接口
                obj = stuStatusRegisterInfoService.add(id, registerInfo);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        //异动信息
        //编辑
        TransferInfoService transferInfoService = new TransferInfoServiceImpl();
        if (bizType.equals(StuStatusBizTypeConstant.TRANSACTION_INFO_BIZ_TYPE_EDIT)) {
            TransferInfo transferInfo = JSON.parseObject(data, TransferInfo.class);
            Long id = transferInfo.getId();
            try {
                obj = transferInfoService.edit(id, transferInfo);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        //添加
        if (bizType.equals(StuStatusBizTypeConstant.TRANSACTION_INFO_BIZ_TYPE_ADD)) {
            TransferInfo transferInfo = JSONObject.parseObject(data, TransferInfo.class);
            Long id = transferInfo.getId();
            try {
                obj = transferInfoService.add(id, transferInfo);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        if (bizType.equals(StuStatusBizTypeConstant.EMPLOYMENT_INFO_BIZ_TYPE_Edit)) {

        }


        return obj;
    }
}